package com.weimob.xh.shiro;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Lipackage com.df.config;
 *
 * import org.apache.shiro.authc.AuthenticationInfo;
 * import org.apache.shiro.authc.AuthenticationToken;
 * import org.apache.shiro.authc.UsernamePasswordToken;
 * import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
 * import org.springframework.context.annotation.Configuration;
 * import org.springframework.stereotype.Component;
 *
 *
 * public class MyCredentialsMatcher extends SimpleCredentialsMatcher {
 *     @Override
 *     public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
 *         UsernamePasswordToken tokenResolve = (UsernamePasswordToken) token;
 *         String tokenPwd = new String(tokenResolve.getPassword());
 *         String infoPwd =(String) info.getCredentials();
 *         //调用当前类重写的equals方法来对比两个password是否一致，返回对比结果
 *         return super.equals(tokenPwd, infoPwd);
 *     }
 * }n
 * @create 2020/7/15
 * @since 1.0.0
 * (功能)：
 */
public class MyRealm extends AuthorizingRealm {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        logger.info("doGetAuthorizationInfo");
        //从主体传过来的认证信息中，获取用户名
        String userName = (String)principalCollection.getPrimaryPrincipal();

        //通过用户名从数据库中获取角色数据
        Set<String> roles = getRolesByUserName(userName);
        //通过用户名从数据库中获取权限数据
        Set<String> permissions = getPermissionsByUserName(userName);

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setStringPermissions(permissions);
        simpleAuthorizationInfo.setRoles(roles);

        return simpleAuthorizationInfo;
    }

    @Override //认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        logger.info("doGetAuthenticationInfo");
        //从主体传过来的认证信息中，获取用户名
        String userName = (String) authenticationToken.getPrincipal();

        //通过用户名从数据库中获得密码
        String password = getPassWordByUserName();
        if(password == null){
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userName,password,getName());
        return authenticationInfo;
    }

    private String getPassWordByUserName() {
        return "123";
    }

    private Set<String> getPermissionsByUserName(String userName) {
        Set<String> sets = new HashSet<String>();
        sets.add("user:add");
        return sets;
    }

    private Set<String> getRolesByUserName(String userName) {
        Set<String> sets = new HashSet<String>();
        sets.add("user");
        return sets;
    }
}
